package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.resp.RespShopCartVo;
import com.goktech.olala.core.service.IShopCartService;
import com.goktech.olala.server.dao.customer.CustomerCollectMapper;
import com.goktech.olala.server.dao.order.ShopCartMapper;
import com.goktech.olala.server.pojo.customer.CustomerCollect;
import com.goktech.olala.server.pojo.order.ShopCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname IShopCartServiceImp
 * @Description TODO
 * @Date 2020/12/21 21:41
 * @Created by ASUS
 */
@Service
public class IShopCartServiceImp implements IShopCartService {

    @Autowired
    ShopCartMapper shopCartMapper;

    @Autowired
    CustomerCollectMapper customerCollectMapper;


    @Override
    public int deleteByPrimaryKey(Integer shopCartId) {
        int i = shopCartMapper.deleteByPrimaryKey(shopCartId);
        return i;
    }

    @Override
    public int insert(ShopCart record) {
        int insert = shopCartMapper.insert(record);
        return insert;
    }

    @Override
    public ShopCart selectByPrimaryKey(Integer shopCartId) {
        ShopCart shopCart = shopCartMapper.selectByPrimaryKey(shopCartId);
        return shopCart;
    }

    @Override
    public List<ShopCart> selectAll() {
        List<ShopCart> shopCartList = shopCartMapper.selectAll();
        return shopCartList;
    }

    @Override
    public int updateByGoodsId(ShopCart record) {
        int i = shopCartMapper.updateByGoodsId(record);
        return i;
    }

    @Override
    public List<ShopCart> selectByCustomerId(String customerId) {
        List<ShopCart> shopCartList = shopCartMapper.selectByCustomerId(customerId);
        return shopCartList;
    }

    //查询购物车入参：展示购物车
    @Override
    public List<RespShopCartVo> shopcartInfo(String customerId) {
        //购物车
        List<ShopCart> shopCarts = shopCartMapper.selectByCustomerId(customerId);
        //收藏夹
        List<CustomerCollect> customerCollects = customerCollectMapper.selectByCustomerId(customerId);

        List<RespShopCartVo> respShopCartVoList = new ArrayList<>();
        if (shopCarts!=null){
            for (int i =0; i<shopCarts.size(); i++){
                RespShopCartVo respShopCartVo = new RespShopCartVo();
                respShopCartVo.setShopCartId(shopCarts.get(i).getShopCartId());
                respShopCartVo.setCustomerId(shopCarts.get(i).getCustomerId());
                respShopCartVo.setGoodsId(shopCarts.get(i).getGoodsId());
                respShopCartVo.setGoodsSn(shopCarts.get(i).getGoodsSn());
                respShopCartVo.setCartType(shopCarts.get(i).getCartType());
                respShopCartVo.setGoodsName(shopCarts.get(i).getGoodsName());
                respShopCartVo.setGoodsAttr(shopCarts.get(i).getGoodsAttr());
                respShopCartVo.setGoodsAmount(shopCarts.get(i).getGoodsAmount());
                respShopCartVo.setShopPrice(shopCarts.get(i).getShopPrice());
                respShopCartVo.setRealBuyPrice(shopCarts.get(i).getRealBuyPrice());
                respShopCartVo.setMarketPrice(shopCarts.get(i).getMarketPrice());

                if (customerCollects!=null){
                    for (int j =0; j<customerCollects.size(); j++){
                        respShopCartVo.setCollectId(customerCollects.get(j).getCollectId());
                        respShopCartVo.setIsCancel(customerCollects.get(j).getIsCancel());
                    }
                }

                respShopCartVoList.add(i,respShopCartVo);
            }
        }

        return respShopCartVoList;
    }

    //查询商品是否在购物车里存在
    @Override
    public ShopCart selectByGoodsId(String goodsId,String customerId) {
        ShopCart shopCart = shopCartMapper.selectByGoodsId(goodsId,customerId);
        return shopCart;
    }

    //添加购物车
    @Override
    public int addShopCart(ShopCart shopCart) {

        //先查询商品是否已经在购物车中
        ShopCart shopCartByGoodsId = shopCartMapper.selectByGoodsId(shopCart.getGoodsId(),shopCart.getCustomerId());

        int i =0;

        //如果已经有该购物车
        if (shopCartByGoodsId!=null){
            //数量++
            int newAmount = shopCart.getGoodsAmount()+shopCartByGoodsId.getGoodsAmount();
            shopCart.setGoodsAmount(newAmount);
            //更新该购物车
            i = shopCartMapper.updateByGoodsId(shopCart);

        }else {
            //第一次添加
            i = shopCartMapper.insert(shopCart);

        }

        return i;
    }
}
